package com.vip.workflow.controller;

import com.vip.common.response.RestResponse;
import com.vip.common.response.RestResponses;
import com.vip.workflow.constant.UrlConsts;
import com.vip.workflow.dto.ProcessDTO;
import com.vip.workflow.service.IProcessService;
import com.vip.workflow.util.InputStreamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 流程控制器
 *
 * @author wgb
 * @date 2020/5/7 18:18
 */
@RestController
@RequestMapping(value = UrlConsts.WORK_FLOW + "/process")
public class ProcessController {

    @Autowired
    private IProcessService processService;

    /**
     * 获取流程列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public RestResponse<List<ProcessDTO>> getProcesslist(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        return RestResponses.newResponseFromResult(processService.getProcessList(pageNo, pageSize));
    }

    /**
     * 通过指定模板部署新流程
     *
     * @param modelId
     * @return
     */
    @PostMapping("/deploy/{modelId}")
    public RestResponse<String> deployNewProcess(@PathVariable String modelId) {
        return RestResponses.newResponseFromResult(processService.deployNewProcessByModelId(modelId));
    }

    /**
     * 指定流程转换为模板
     *
     * @param processId 流程ID
     * @return
     */
    @PostMapping("/ctm/{processId}")
    public RestResponse<String> convertToModel(@PathVariable String processId) {
        return RestResponses.newResponseFromResult(processService.processConvertToModel(processId));
    }

    /**
     * 删除指定流程
     *
     * @param id 流程ID
     * @return
     */
    @DeleteMapping("/{id}")
    public RestResponse<String> deleteProcess(@PathVariable String id) {
        return RestResponses.newResponseFromResult(processService.deleteProcessById(id));
    }

    /**
     * 批量删除流程
     *
     * @param ids 流程ID
     * @return
     */
    @DeleteMapping("/multi")
    public RestResponse<String> deleteProcess(@RequestBody List<String> ids) {
        return RestResponses.newResponseFromResult(processService.deleteMultiProcessById(ids));
    }

    /**
     * 获取指定流程的XML
     *
     * @param id
     * @param response
     */
    @GetMapping(value = "/resource/xml/{id}")
    public void resourceReadXml(@PathVariable String id, HttpServletResponse response) throws Exception {
        InputStream resourceAsStream = processService.resourceReadByTypeAndId("xml", id);
        byte[] b = new byte[1024];
        int len;
        while ((len = resourceAsStream.read(b, 0, 1024)) != -1) {
            response.setHeader("Content-type", "text/plain;charset=UTF-8");
            response.getOutputStream().write(b, 0, len);
        }
    }

    /**
     * 获取指定流程的图片
     *
     * @param id
     */
    @GetMapping(value = "/resource/image/{id}")
    public ResponseEntity<byte[]> resourceReadImage(@PathVariable String id) throws Exception {
        InputStream resourceAsStream = processService.resourceReadByTypeAndId("image", id);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG);
        return new ResponseEntity<>(InputStreamUtils.inputStreamToByte(resourceAsStream), headers, HttpStatus.OK);
    }

    /**
     * 上传并部署流程
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public RestResponse<String> uploadAndDeploy(MultipartFile file) {
        return RestResponses.newResponseFromResult(processService.uploadAndDeploy(file));
    }
}
